package com.android.videolib;

import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.os.Build;

import com.photoeditor.demo.util.Gif2VideoUtils;

import java.io.IOException;

import static android.media.MediaFormat.MIMETYPE_VIDEO_AVC;

/**
 * Created by @author winn on 18/6/20.
 */

public class MediaCodecHelper {

    private final static int VIDEO_MAX_OFFSET = 50;

    private MediaCodecInfo mMediaCodecInfo;

    private static MediaCodecHelper sInstance;

    public static MediaCodecHelper getInstance() {
        if (sInstance == null) {
            synchronized (MediaCodecHelper.class) {
                if (sInstance == null) {
                    sInstance = new MediaCodecHelper();
                }
            }
        }
        return sInstance;
    }

    private MediaCodecHelper() {
    }

    public void init() {
        try {
            MediaCodec encoder = MediaCodec.createEncoderByType(MIMETYPE_VIDEO_AVC);
            mMediaCodecInfo = encoder.getCodecInfo();
            encoder.release();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean isResolutionSupported(int videoWidth, int videoHeight) {
        MediaCodecInfo.CodecCapabilities capabilities = mMediaCodecInfo.getCapabilitiesForType(MIMETYPE_VIDEO_AVC);
        MediaCodecInfo.VideoCapabilities videoCapabilities = capabilities.getVideoCapabilities();
        return videoCapabilities.isSizeSupported(videoWidth, videoHeight);
    }

    /**
     * 固定高度，获取系统支持的视频宽度
     * @param videoWidth
     * @param videoHeight
     * @return
     */
    public int getSupportVideoWidth(int videoWidth, int videoHeight) {
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            return Gif2VideoUtils.verifyVideoOutPutSize(videoWidth);
        }
        int ret = videoWidth;
        boolean support = isResolutionSupported(videoWidth, videoHeight);
        int offset = 0;
        while (!support && offset < VIDEO_MAX_OFFSET) {
            ++offset;
            int tmp = videoWidth - offset;
            ret = tmp;
            support = isResolutionSupported(tmp, videoHeight);
            if (support) {
                break;
            }

            tmp = videoWidth + offset;
            ret = tmp;
            support = isResolutionSupported(tmp, videoHeight);
        }
        if (ret >= VIDEO_MAX_OFFSET) {
            ret = Gif2VideoUtils.verifyVideoOutPutSize(videoWidth);
        }
        return ret;
    }

    /**
     * 固定宽度，获取系统支持的视频高度
     * @param videoWidth
     * @param videoHeight
     * @return
     */
    public int getSupportVideoHeight(int videoWidth, int videoHeight) {
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            return Gif2VideoUtils.verifyVideoOutPutSize(videoHeight);
        }
        int ret = videoHeight;
        boolean support = isResolutionSupported(videoWidth, videoHeight);
        int offset = 0;
        while (!support && offset < VIDEO_MAX_OFFSET) {
            ++offset;
            int tmp = videoHeight - offset;
            ret = tmp;
            support = isResolutionSupported(videoWidth, tmp);
            if (support) {
                break;
            }

            tmp = videoHeight + offset;
            ret = tmp;
            support = isResolutionSupported(videoWidth, videoHeight);
        }
        if (offset >= VIDEO_MAX_OFFSET) {
            ret = Gif2VideoUtils.verifyVideoOutPutSize(videoWidth);
        }
        return ret;
    }


}
